<template>
  <div class="container">
    <div class="search-box">
      <el-input placeholder="请输入内容" class="input-box" v-model="name"></el-input>
      <el-button @click="clear">清除</el-button>
      <el-button type="primary" @click="search">搜索</el-button>
    </div>
    <el-table :data="list" border style="width: 100%">
      <el-table-column type="index" label="任务编号" width="80"> </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click="edit(row.id)">编辑</el-button>
          <el-popconfirm
            @confirm="del(row.id)"
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        @current-change="changePage"
        :page-size="params._limit"
        :total="total"
      >
      </el-pagination>
      <el-button size="small" plain @click="addDialog">+ 新增</el-button>
    </div>
    <el-dialog title="提示" :visible="dialogVisible" @close="closeDialog" width="30%">
      <el-form label-width="100px" :model="formData" :rules="rules" ref="form">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="formData.des" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getListAPI, addAPI, getDetailAPI, updateDetailAPI, delAPI, searchAPI } from './api'
import { v4 as uuidv4 } from 'uuid'
export default {
  data() {
    return {
      list: [],
      formData: {
        name: '',
        des: ''
      },
      name: '',
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      total: null,
      page: '',
      dialogVisible: false,
      rules: {
        name: [{ required: true, message: '任务名称必填', trigger: 'blur' }],
        des: [{ required: true, message: '任务描述必填', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    async getList() {
      const res = await getListAPI(this.params)
      console.log(res)
      this.list = res.data
      this.total = Number(res.headers['x-total-count'])
    },
    add() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.formData.id) {
            await updateDetailAPI(this.formData)
            this.$message.success('更新成功')
          } else {
            await addAPI({ ...this.formData, id: uuidv4() })
            this.$message.success('添加成功')
          }
          this.dialogVisible = false
          this.resetForm()
          this.getList()
        }
      })
    },
    async edit(id) {
      this.dialogVisible = true
      const res = await getDetailAPI(id)
      this.formData = { ...res.data, id }
    },
    async del(id) {
      await delAPI(id)
      this.$message.success('删除成功')
      this.getList()
    },
    async search() {
      const res = await searchAPI(this.name)
      console.log(res)
      this.list = res.data
    },
    clear() {
      this.name = ''
      this.getList()
    },
    resetForm() {
      this.$refs.form.resetFields()
      this.formData.id = ''
    },
    changePage(page) {
      this.params._page = page
      this.getList()
    },
    addDialog() {
      this.dialogVisible = true
    },
    closeDialog() {
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
